<!DOCTYPE html>
<html lang="en">
  <head>
    <title>BodyParser Class Reference</title>
    <link rel="stylesheet" type="text/css" href="../css/jazzy.css" />
    <link rel="stylesheet" type="text/css" href="../css/highlight.css" />
    <meta charset="utf-8">
    <script src="../js/jquery.min.js" defer></script>
    <script src="../js/jazzy.js" defer></script>
    
    <script src="../js/lunr.min.js" defer></script>
    <script src="../js/typeahead.jquery.js" defer></script>
    <script src="../js/jazzy.search.js" defer></script>
  </head>
  <body>

    <a name="//apple_ref/swift/Class/BodyParser" class="dashAnchor"></a>

    <a title="BodyParser Class Reference"></a>

    <header class="header">
      <p class="header-col header-col--primary">
        <a class="header-link" href="../index.html">
          Kitura Docs
        </a>
         (99% documented)
      </p>
    
      <p class="header-col--secondary">
        <form role="search" action="../search.json">
          <input type="text" placeholder="Search documentation" data-typeahead>
        </form>
      </p>
    
        <p class="header-col header-col--secondary">
          <a class="header-link" href="https://github.com/Kitura/Kitura/">
            <img class="header-icon" src="../img/gh.png"/>
            View on GitHub
          </a>
        </p>
    
    </header>

    <p class="breadcrumbs">
      <a class="breadcrumb" href="../index.html">Kitura Reference</a>
      <img class="carat" src="../img/carat.png" />
      BodyParser Class Reference
    </p>

    <div class="content-wrapper">
      <nav class="navigation">
        <ul class="nav-groups">
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Classes.html">Classes</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/BodyParser.html">BodyParser</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/BodyParserMultiValue.html">BodyParserMultiValue</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/ContentType.html">ContentType</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/Kitura.html">Kitura</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/Router.html">Router</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/RouterMiddlewareGenerator.html">RouterMiddlewareGenerator</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/RouterRequest.html">RouterRequest</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/RouterResponse.html">RouterResponse</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/StaticFileServer.html">StaticFileServer</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/StaticFileServer/CacheOptions.html">– CacheOptions</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Classes/StaticFileServer/Options.html">– Options</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Enums.html">Enumerations</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Enums/JSONPError.html">JSONPError</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Enums/LinkParameter.html">LinkParameter</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Enums/ParsedBody.html">ParsedBody</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Enums/RouterMethod.html">RouterMethod</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Extensions.html">Extensions</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Extensions/DecodingError.html">DecodingError</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Protocols.html">Protocols</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Protocols/ResponseHeadersSetter.html">ResponseHeadersSetter</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Protocols/RouterMiddleware.html">RouterMiddleware</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Protocols/TypeSafeMiddleware.html">TypeSafeMiddleware</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Structs.html">Structures</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/AdditionalCookieAttribute.html">AdditionalCookieAttribute</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/CodableHelpers.html">CodableHelpers</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/HTTPVersion.html">HTTPVersion</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/Headers.html">Headers</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/MediaType.html">MediaType</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/MediaType/TopLevelType.html">– TopLevelType</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/Part.html">Part</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/Part/HeaderType.html">– HeaderType</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/SSLConfig.html">SSLConfig</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Structs/SwaggerDocument.html">SwaggerDocument</a>
              </li>
            </ul>
          </li>
          <li class="nav-group-name">
            <a class="nav-group-name-link" href="../Typealiases.html">Type Aliases</a>
            <ul class="nav-group-tasks">
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Typealiases.html#/s:6Kitura14HTTPStatusCodea">HTTPStatusCode</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Typealiases.html#/s:6Kitura16LifecycleHandlera">LifecycleHandler</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Typealiases.html#/s:6Kitura12RequestErrora">RequestError</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Typealiases.html#/s:6Kitura13RouterHandlera">RouterHandler</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Typealiases.html#/s:6Kitura22RouterParameterHandlera">RouterParameterHandler</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Typealiases.html#/s:6Kitura13ServerOptionsa">ServerOptions</a>
              </li>
              <li class="nav-group-task">
                <a class="nav-group-task-link" href="../Typealiases.html#/s:6Kitura17WrittenDataFiltera">WrittenDataFilter</a>
              </li>
            </ul>
          </li>
        </ul>
      </nav>
      <article class="main-content">

        <section class="section">
          <div class="section-content top-matter">
            <h1>BodyParser</h1>
              <div class="declaration">
                <div class="language">
                  
                  <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">class</span> <span class="kt">BodyParser</span> <span class="p">:</span> <span class="kt"><a href="../Protocols/RouterMiddleware.html">RouterMiddleware</a></span></code></pre>

                </div>
              </div>
            <p>The <code>BodyParser</code> parses the body of the request prior to sending it to the handler. It reads the Content-Type of the message header and populates the <code><a href="../Classes/RouterRequest.html">RouterRequest</a></code> body field with a corresponding <code><a href="../Enums/ParsedBody.html">ParsedBody</a></code> enumeration.</p>

<p>In order for the BodyParser to be used it must first be registered with any routes that are interested in the <code><a href="../Enums/ParsedBody.html">ParsedBody</a></code> payload.</p>
<h3 id='parsedbody-enumeration' class='heading'>ParsedBody enumeration:</h3>

<p>The mappings from the incoming Content-Type to an internal representation of the body are as follows:</p>
<pre class="highlight swift"><code>   <span class="o">.</span><span class="nf">json</span><span class="p">([</span><span class="kt">String</span><span class="p">:</span> <span class="kt">Any</span><span class="p">])</span>          <span class="c1">// "application/json"</span>
   <span class="o">.</span><span class="nf">text</span><span class="p">(</span><span class="kt">String</span><span class="p">)</span>                 <span class="c1">// "text/*"</span>
   <span class="o">.</span><span class="nf">urlEncoded</span><span class="p">([</span><span class="kt">String</span><span class="p">:</span><span class="kt">String</span><span class="p">])</span>  <span class="c1">// "application/x-www-form-urlencoded"</span>
   <span class="o">.</span><span class="nf">multipart</span><span class="p">([</span><span class="kt">Part</span><span class="p">])</span>            <span class="c1">// "multipart/form-data"</span>
   <span class="o">.</span><span class="nf">raw</span><span class="p">(</span><span class="kt">Data</span><span class="p">)</span>                    <span class="c1">// Any other Content-Type</span>
</code></pre>

<p>Each case has a corresponding convenience property, e.g. <code>asURLEncoded: [String:String]</code>, for accessing the associated data.</p>

<p><strong>Note</strong>: If you have not declared a Content-Type header, <code><a href="../Enums/ParsedBody.html">ParsedBody</a></code> will be <code>nil</code>.</p>
<h3 id='usage-example' class='heading'>Usage Example:</h3>

<p>In this example, all routes to the BodyParser middleware are registered to the <code>BodyParser</code> middleware. A request with &ldquo;application/json&rdquo;, ContentType header is received. It is then parsed as JSON and the value for &ldquo;name&rdquo; is returned in the response.</p>
<pre class="highlight swift"><code><span class="n">router</span><span class="o">.</span><span class="nf">all</span><span class="p">(</span><span class="s">"/name"</span><span class="p">,</span> <span class="nv">middleware</span><span class="p">:</span> <span class="kt">BodyParser</span><span class="p">())</span>
<span class="n">router</span><span class="o">.</span><span class="nf">post</span><span class="p">(</span><span class="s">"/name"</span><span class="p">)</span> <span class="p">{</span> <span class="n">request</span><span class="p">,</span> <span class="n">response</span><span class="p">,</span> <span class="n">next</span> <span class="k">in</span>
    <span class="k">guard</span> <span class="k">let</span> <span class="nv">jsonBody</span> <span class="o">=</span> <span class="n">request</span><span class="o">.</span><span class="n">parsedBody</span><span class="p">?</span><span class="o">.</span><span class="n">asJSON</span> <span class="k">else</span> <span class="p">{</span>
        <span class="nf">next</span><span class="p">()</span>
        <span class="k">return</span>
    <span class="p">}</span>
    <span class="k">let</span> <span class="nv">name</span> <span class="o">=</span> <span class="n">jsonBody</span><span class="p">[</span><span class="s">"name"</span><span class="p">]</span> <span class="k">as?</span> <span class="kt">String</span> <span class="p">??</span> <span class="s">""</span>
    <span class="k">try</span> <span class="n">response</span><span class="o">.</span><span class="nf">send</span><span class="p">(</span><span class="s">"Hello </span><span class="se">\(</span><span class="n">name</span><span class="se">)</span><span class="s">"</span><span class="p">)</span><span class="o">.</span><span class="nf">end</span><span class="p">()</span>
<span class="p">}</span>
</code></pre>

<p><strong>Note</strong>: When using Codable Routing in Kitura 2.x the BodyParser should not be registered to any codable routes (doing so will log the following error &ldquo;No data in request. Codable routes do not allow the use of a BodyParser.&rdquo; and the route handler will not be executed).</p>

          </div>
        </section>

        <section class="section">
          <div class="section-content">
            <div class="task-group">
              <ul class="item-container">
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:6Kitura10BodyParserCACycfc"></a>
                    <a name="//apple_ref/swift/Method/init()" class="dashAnchor"></a>
                    <a class="token" href="#/s:6Kitura10BodyParserCACycfc">init()</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Initializes a BodyParser instance.
 Needed since default initalizer is internal.</p>
<h3 id='usage-example' class='heading'>Usage Example:</h3>
<pre class="highlight swift"><code> <span class="k">let</span> <span class="nv">middleware</span> <span class="o">=</span> <span class="kt">BodyParser</span><span class="p">()</span>
</code></pre>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="nf">init</span><span class="p">()</span></code></pre>

                        </div>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:6Kitura10BodyParserC6handle7request8response4nextyAA13RouterRequestC_AA0H8ResponseCyyctKF"></a>
                    <a name="//apple_ref/swift/Method/handle(request:response:next:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:6Kitura10BodyParserC6handle7request8response4nextyAA13RouterRequestC_AA0H8ResponseCyyctKF">handle(request:<wbr>response:<wbr>next:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>This function is called by the Kitura <code><a href="../Classes/Router.html">Router</a></code> when an incoming request matches the route provided when the BodyParser was registered with the <code><a href="../Classes/Router.html">Router</a></code>. It performs the parsing of the body content using <code>parse(_:contentType)</code>. We don&rsquo;t expect a user to call this function directly.</p>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">func</span> <span class="nf">handle</span><span class="p">(</span><span class="nv">request</span><span class="p">:</span> <span class="kt"><a href="../Classes/RouterRequest.html">RouterRequest</a></span><span class="p">,</span> <span class="nv">response</span><span class="p">:</span> <span class="kt"><a href="../Classes/RouterResponse.html">RouterResponse</a></span><span class="p">,</span> <span class="nv">next</span><span class="p">:</span> <span class="kd">@escaping</span> <span class="p">()</span> <span class="o">-&gt;</span> <span class="kt">Void</span><span class="p">)</span> <span class="k">throws</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>request</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code><a href="../Classes/RouterRequest.html">RouterRequest</a></code> object used to work with the incoming
                HTTP request.</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>response</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The <code><a href="../Classes/RouterResponse.html">RouterResponse</a></code> object used to respond to the
                HTTP request.</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>next</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The closure called to invoke the next handler or middleware
                associated with the request.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:6Kitura10BodyParserC5parse_11contentTypeAA06ParsedB0OSgAA13RouterRequestC_SSSgtFZ"></a>
                    <a name="//apple_ref/swift/Method/parse(_:contentType:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:6Kitura10BodyParserC5parse_11contentTypeAA06ParsedB0OSgAA13RouterRequestC_SSSgtFZ">parse(_:<wbr>contentType:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>This function is called by the Kitura <code><a href="../Classes/Router.html">Router</a></code> when an incoming request matches the route provided when the BodyParser was registered with the <code><a href="../Classes/Router.html">Router</a></code>. The <code>middleware.handle(...)</code> function will parse the body content of an incoming request using this function. A user can call this function directly but ordinarily won&rsquo;t need to.</p>
<h3 id='usage-example' class='heading'>Usage Example:</h3>

<p>In this example, the body of the request is parsed to be of the passed in contentType.</p>
<pre class="highlight swift"><code><span class="n">request</span><span class="o">.</span><span class="n">body</span> <span class="o">=</span> <span class="kt">BodyParser</span><span class="o">.</span><span class="nf">parse</span><span class="p">(</span><span class="n">request</span><span class="p">,</span> <span class="nv">contentType</span><span class="p">:</span> <span class="n">contentType</span><span class="p">)</span>
</code></pre>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">class</span> <span class="kd">func</span> <span class="nf">parse</span><span class="p">(</span><span class="n">_</span> <span class="nv">message</span><span class="p">:</span> <span class="kt"><a href="../Classes/RouterRequest.html">RouterRequest</a></span><span class="p">,</span> <span class="nv">contentType</span><span class="p">:</span> <span class="kt">String</span><span class="p">?)</span> <span class="o">-&gt;</span> <span class="kt"><a href="../Enums/ParsedBody.html">ParsedBody</a></span><span class="p">?</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>message</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>Message coming from the socket.</p>
                                </div>
                              </td>
                            </tr>
                            <tr>
                              <td>
                                <code>
                                <em>contentType</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The content type as a String.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>The parsed body.</p>
                      </div>
                    </section>
                  </div>
                </li>
                <li class="item">
                  <div>
                    <code>
                    <a name="/s:6Kitura10BodyParserC04readB4Data4with10Foundation0E0VAA13RouterRequestC_tKFZ"></a>
                    <a name="//apple_ref/swift/Method/readBodyData(with:)" class="dashAnchor"></a>
                    <a class="token" href="#/s:6Kitura10BodyParserC04readB4Data4with10Foundation0E0VAA13RouterRequestC_tKFZ">readBodyData(with:<wbr>)</a>
                    </code>
                  </div>
                  <div class="height-container">
                    <div class="pointer-container"></div>
                    <section class="section">
                      <div class="pointer"></div>
                      <div class="abstract">
                        <p>Read the body data of the request.</p>
<h3 id='usage-example' class='heading'>Usage Example:</h3>

<p>In this example, the body of the request is read into a constant (called bodyData) using an instance of <code><a href="../Classes/RouterRequest.html">RouterRequest</a></code> (called request).</p>
<pre class="highlight swift"><code><span class="k">let</span> <span class="nv">bodyData</span> <span class="o">=</span> <span class="k">try</span> <span class="nf">readBodyData</span><span class="p">(</span><span class="nv">with</span><span class="p">:</span> <span class="n">request</span><span class="p">)</span>
</code></pre>
<div class="aside aside-throws">
    <p class="aside-title">Throws</p>
    Socket.Error if an error occurred while reading from a socket.

</div>

                      </div>
                      <div class="declaration">
                        <h4>Declaration</h4>
                        <div class="language">
                          <p class="aside-title">Swift</p>
                          <pre class="highlight swift"><code><span class="kd">public</span> <span class="kd">class</span> <span class="kd">func</span> <span class="nf">readBodyData</span><span class="p">(</span><span class="n">with</span> <span class="nv">reader</span><span class="p">:</span> <span class="kt"><a href="../Classes/RouterRequest.html">RouterRequest</a></span><span class="p">)</span> <span class="k">throws</span> <span class="o">-&gt;</span> <span class="kt">Data</span></code></pre>

                        </div>
                      </div>
                      <div>
                        <h4>Parameters</h4>
                        <table class="graybox">
                          <tbody>
                            <tr>
                              <td>
                                <code>
                                <em>with</em>
                                </code>
                              </td>
                              <td>
                                <div>
                                  <p>The socket reader.</p>
                                </div>
                              </td>
                            </tr>
                          </tbody>
                        </table>
                      </div>
                      <div>
                        <h4>Return Value</h4>
                        <p>The body data associated with the request.</p>
                      </div>
                    </section>
                  </div>
                </li>
              </ul>
            </div>
          </div>
        </section>

      </article>
    </div>
    <section class="footer">
      <p>&copy; 2021 <a class="link" href="" target="_blank" rel="external">IBM &amp; Kitura project authors</a>. All rights reserved. (Last updated: 2021-01-25)</p>
      <p>Generated by <a class="link" href="https://github.com/realm/jazzy" target="_blank" rel="external">jazzy ♪♫ v0.13.6</a>, a <a class="link" href="https://realm.io" target="_blank" rel="external">Realm</a> project.</p>
    </section>
  </body>
</div>
</html>
